  // pages/servering/servering.js
  const app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    reportContent: '', //施工报告内容
    imgs: '', //施工报告图片
    des: '', //施工报告内容
    commentContent: '', //评论内容
    star: 5, //评价星星数量
    orderOpen: false,
    open: false,
    btn: "确认师傅接单",
    status: 1,
    time: "",
    statusList: [],
    affirm: 0,
    orderId: "",
    demo: 2,
    totalFee: "",
    showModal: true,
    feeItemIds: [],
    serviceFee: "",
    feeOpen: false,
    distance: 0,
    getTime: [],
    // sendClick: 0,
    statusName: "",
    receiveUserId: 0,
    pay: [],
    sceneImg:[],
    orderList: [{
      id: 1,
      orderId: "XZQN5656121143466",
      orderBigClass: "视频监控",
      orderSmallClass: "视频无图像",
      receiving: "服务中",
      orderDistance: "12",
      orderPosition: "宜丰县人民法院",
      orderProblem: "宜丰县人民法院监控设备，没有图像",
      orderPrices: "16480",
      orderTime: "2019年4月21日 11：07",
      state: "确认完成"
    }],

    processData: [{
      name: '发单成功',
      // start: '../../icons/lin1.png',
      end: '../../icons/lin1.png',
      icon: '../../icons/l7.png',
      time: "",
      
    },
    {
      name: '已接单',
      start: '../../icons/lin2.png',
      end: '../../icons/lin2.png',
      icon: '../../icons/l8.png',
      time: ""
    },
    {
      name: '等待发单方确认',
      start: '../../icons/lin2.png',
      end: '../../icons/lin2.png',
      icon: '../../icons/l8.png',
      time: ""
    },
    {
      name: '师傅正在赶往途中',
      start: '../../icons/lin2.png',
      end: '../../icons/lin2.png',
      icon: '../../icons/l8.png',
      time: ""
    },
    {
      name: '师傅开始施工',
      start: '../../icons/lin2.png',
      end: '../../icons/lin2.png',
      icon: '../../icons/l8.png',
      time: ""
    },
    {
      name: '师傅施工已完成',
      start: '../../icons/lin2.png',
      end: '../../icons/lin2.png',
      icon: '../../icons/l8.png',
      time: ""
    },
    {
      name: '订单已完成',
      start: '../../icons/lin2.png',
      icon: '../../icons/l8.png',
      end: '../../icons/lin2.png',
      time: ""
    }
    ],
    projectMes: [],
    msgList: [{
      titlePerson: "个人",
      number: "王师傅 164998800022",
      titleAddress: "服务地区",
      address: "南昌县",
      titleRecommend: "服务介绍",
      recommendFirst: "视频监控、门禁",
      recommendSecond: "专注弱电服务三十年，完成数百个大小企业的项目，具有整套的服务流程。",
      titlePromise: "服务承诺",
      promise: [{
        promise: "1.三公里内不收服务费"
      },
      {
        promise: "2.接单1天内处理"
      },
      {
        promise: "3.空跑费50元/次"
      },
      {
        promise: "4.安装后一个月内免检修费"
      }
      ]
    }],

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    var that = this;
    this.setData({
      orderId: options.userId,
      receiveUserId: options.receiveUserId
    });
    console.log(this.data.orderId)
    console.log(this.data.receiveUserId);
    if (this.data.getTime == "") {
      wx.showLoading({
        title: "加载中",
        mask: true,
        success: function (res) {
          // that.setData({
          //   page: false
          // })
        }
      });
    }
    this.beforeWorkPic();
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.myData();
    this.beforeWork();
  },

  /**
   * 图片预览
   */
  imgPreview: function (e) {
    let img = e.currentTarget.dataset.src;
    console.log(img)
    wx.previewImage({
      urls: [img],
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 点击接单事件
  grabOrder: function () {
    var that = this;
    if (this.data.statusName == "确认师傅接单") {
      // that.change();
      wx.showModal({
        title: '提示',
        content: '确认由该师傅接单',
        success: function (res) {
          console.log(res);
          if (res.confirm) {
            if (that.data.orderId == "" || that.data.orderId == undefined) {
              that.setData({
                orderId: wx.getStorageSync("orderId")
              })
              
            }

            wx.request({
              url: 'https://www.xiaozheng8.com/serviceOrder/setStatus/v3',
              method: "post",
              data: {
                orderId: that.data.orderId,
                status: that.data.status,

              },
              success: function (res) {

                that.myData();
                that.myLog()
              }

            })
          }
        }
      })
    }

    if (this.data.statusName == "确认完成") {
      // that.getComment();
      that.showReport();
      wx.showModal({
        title: '提示',
        content: '确认完成订单',
        cancelText: "取消",
        confirmText: "确认",
        success: function (res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '../payment/payment?orderId=' + that.data.orderId + "&receiveUserId=" + that.data.receiveUserId + "&price=" + that.data.price + "&status=" + that.data.status,
            })
          }
          that.myData();

        }

      })
    } else if (that.data.status >= 11) {
      that.setData({
        btn: "已结束"
      })

     that.getComment();

    }


  },
  // 收起信息
  packUp: function (options) {
    console.log(options);
    this.setData({
      open: !this.data.open
    })
  },
  orderInfo: function () {
    this.setData({
      orderOpen: !this.data.orderOpen,

    })
  },
  // 价格明细
  priceDetail: function (e) {
    this.setData({
      showModal: true
    })
  },
  feeInfo: function () {
    this.setData({
      feeOpen: !this.data.feeOpen,
    })
  },
  // 举报
  report: function () {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '是否确认要举报该服务商',
      cancelText: "取消",
      confirmText: "确认",
      success: function (res) {
        if (res.confirm) {
          wx.navigateTo({
            url: '../complain/complain?orderId=' + that.data.orderId,
          })
        }
      }
    })
  },
  // 施工前报告
  beforeWork: function () {
    var that = this;
    wx.request({
      url: 'https://www.xiaozheng8.com/service/order/findImg',
      data: {
        orderId: that.data.orderId
      },
      method: 'post',
      success: function (res) {
        console.log(res);
        var pic = res.data.data.images.split("@");
        for(var i in pic){
          if(pic[i]==""){
             pic.splice(i,1)
          }
        }
        console.log(pic); 
        that.setData({
          location: res.data.data.address,
          picArr: pic
        })

      
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },

  // 取消订单
  cancel: function () {
    wx.showModal({
      title: "提示",
      content: '您当前不能直接取消订单，若要取消订单请联系客服',
      confirmText: "确定",
      showCancel: false
    })
  },
  // 拨打电话
  call: function () {
    var that = this;
    console.log(that.data.msgList[0].phone);
    wx.makePhoneCall({
      phoneNumber: that.data.msgList[0].phone,
    })
  },
  // 获取数据
  myData: function () {
    var that = this;

    if (this.data.orderId == "" || this.data.orderId == undefined || this.data.orderId == null) {
      that.setData({
        orderId: wx.getStorageSync("orderId")

      })
      console.log("最终本地获取" + that.data.orderId);
    }
    if (that.data.orderId == "" || that.data.orderId == undefined) {
      orderId: wx.getStorageSync("orderId");
    }
    // 获取数据
    wx.request({
      url: 'https://www.xiaozheng8.com/serviceOrder/sendFindById',
      method: "post",
      data: {
        orderId: that.data.orderId,

      },

      success: function (res) {
        console.log(res);
        that.setData({
          status: res.data.data.status,
          orderList: [res.data.data],
          price: res.data.data.totalFee,
          distance: parseInt(res.data.data.distance / 1000),
          projectMes: res.data.data.project,
          statusName: res.data.data.statusName,
          sendClick: res.data.data.sendClick,
          lat:res.data.data.lat,
          lng:res.data.data.lng
        })
        var fee = res.data.data.feeItemIds;
        that.data.feeItemIds.push(fee.split(" "));
        console.log(that.data.distance);
        that.setData({
          feeItemIds: that.data.feeItemIds,

        })
        console.log(that.data.projectMes)
        console.log(that.data.status + "状态");

        wx.request({
          url: 'https://www.xiaozheng8.com/serviceOrder/log/v2',
          method: "post",
          data: {
            orderId: that.data.orderId,
            userType: 0
          },
          success: function (res) {
            console.log(res.data.data)
            for (var i = 0; i < res.data.data.length; i++) {
              if (res.data.data.length != that.data.processData.length) {
                var addList = '{"name": "正在施工","start": "../../icons/lin2.png","end":"../../icons/lin2.png","icon": "../../icons/l8.png","time": ""} ';
                console.log(JSON.parse(addList));
                that.data.processData.push(JSON.parse(addList));
                that.setData({
                  processData: that.data.processData
                })

                console.log(that.data.processData);
                console.log("%c", "color: red;background: yellow;", res.data.data);
                that.myLog();



              }
            }
            for (var i = 0; i < res.data.data.length; i++) {
              if (i + 1 != res.data.data.length) {

                if (res.data.data[i].isOk == true && res.data.data[i + 1].isOk == false) {
                  console.log(res.data.data.length);
                  console.log(i);

                  that.data.processData[i].start = '../../icons/lin1.png';
                  that.data.processData[i].end = '../../icons/lin1.png';
                  that.data.processData[i].icon = '../../icons/l7.png';
                  that.data.processData[i + 1].start = '../../icons/lin1.png';
                  that.data.processData[i + 1].end = '../../icons/lin1.png';
                  that.data.processData[i + 1].icon = '../../icons/l3.png';
                  that.setData({
                    processData: that.data.processData
                  });
                  for (var j = 0; j < i; j++) {
                    that.data.processData[j].start = '../../icons/lin1.png';
                    that.data.processData[j].end = '../../icons/lin1.png';
                    that.data.processData[j].icon = '../../icons/l7.png';
                    that.setData({
                      processData: that.data.processData,
                      top: res.data.msg
                    });
                  }
                }
              } else if (i + 1 == res.data.data.length && res.data.data[i].isOk == true) {
                for (var j = 0; j < i; j++) {
                  that.data.processData[j].start = '../../icons/lin1.png';
                  that.data.processData[j].end = '../../icons/lin1.png';
                  that.data.processData[j].icon = '../../icons/l7.png';
                  that.setData({
                    processData: that.data.processData
                  });
                }
                that.data.processData[i].start = '../../icons/lin1.png';
                that.data.processData[i].end = '../../icons/lin1.png';
                that.data.processData[i].icon = '../../icons/l7.png';
                that.setData({
                  processData: that.data.processData
                })

              }
            }
            that.data.processData[0].start = "",
              that.data.processData[that.data.processData.length - 1].end = '',
              that.setData({
                processData: that.data.processData
              });
              console.log(that.data.status);
              console.log("这是状态。。。。。。。。")
            if(that.data.status>=8){
              that.beforeWork()
            }
            // 状态码大于10显示报告
            if (that.data.status >= 10) {
              that.showReport();

            }
            if (that.data.status >= 11) {
              that.getComment();
              // that.showReport();
            }

            that.showWindow();
            that.myLog();

          }
        })

      }

    });
    wx.request({
      url: 'https://www.xiaozheng8.com/receiveUser/findByReceiveUserId',
      method: "post",
      data: {
        receiveUserId: that.data.receiveUserId
      },
      success: function (res) {
        console.log(res);
        that.setData({
          msgList: [res.data.data],
          // projectMes: res.data.data.project
        })
        // console.log(that.data.projectMes)
      }


    });


  },
  // 如果状态码大于10时，显示施工报告
  showReport: function () {
    var that = this;
    console.log(that.data.orderId);
    wx.request({
      url: 'https://www.xiaozheng8.com/report/findByOrderId',
      data: {
        orderId: that.data.orderId,
      },
      method: 'post',
      success: function (res) {
        // console.log("s和igong")
        console.log(res);
        that.setData({
          reportContent: res.data.data.content,
          imgs: res.data.data.imgs,
          des: res.data.data.des
        })
        console.log(that.data.picUrl)
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  // 获取支付回执
  pay: function () {
    var that = this
    wx.request({
      url: 'https://www.xiaozheng8.com/money/record/findByOrderId',
      data: {
        orderId: that.data.orderId
      },
      method: 'post',
      success: function (res) {
        console.log(res);
        that.setData({
          pay: [res.data.data]
        })
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  /**
   *获取评价
   */
  getComment: function () {
    let that = this;
    wx.request({
      url: 'https://www.xiaozheng8.com/service/comment/findById',
      method: 'post',
      data: {
        orderId: that.data.orderId
      },
      success: function (res) {
        console.log(res)
        console.log("---res--")
        that.setData({
          commentContent: res.data.data.content,
          star: res.data.data.grade,

        })
        console.log(that.data.commentContent)
      }
    })
  },

  // 是否显示上面窗口
  showWindow: function () {
    var that = this;
    for (var i = 0; i < that.data.processData.length; i++) {
      if (that.data.status == 6) {
        that.setData({
          btn: "等待师傅出发",
          affirm: 1

        })

      } else if (that.data.status == 7) {
        that.setData({
          btn: "师傅正在途中",
          affirm: 1

        })

      } else if (that.data.status == 8 || that.data.status == 9) {
        that.setData({
          btn: "已开始施工",
          affirm: 1
        })

      } else if (that.data.status == 10) {
        that.setData({
          btn: "确认完成",
          affirm: 0
        })
        // that.getComment();
        // that.showReport();

      } else if (that.data.status > 10) {
        that.setData({
          btn: "已结束",
          affirm: 0
        })
        that.getComment();
        that.showReport();
        that.pay();
      }



    }

  },
  // 如果上传了施工前图片，显示施工现场图片
  beforeWorkPic(){
    var that=this;
    wx.request({
      url: 'https://www.xiaozheng8.com/order/img/findByOrderId',
      data: {
        orderId:that.data.orderId
      },
      method: 'post',
      success: (res) => {
       console.log(res); 
       that.setData({
        sceneImg:res.data.data
       })
      },
      fail: (res) => {
        console.log(res);
      },
      complete: () => {}
    });
      
  },
  // 位置跟踪

  map: function () {
    let that = this;
    wx.getSetting({
      success: (res) => {
        console.log(res);
        if (!res.authSetting['scope.userLocation'])
          app.openConfirm()
      }, fail(res) {
        console.log(res);
      
      }
    })
    // 查找接单方的位置
    wx.request({
      url: 'https://www.xiaozheng8.com/receiveUser/findByReceiveUserId',
      method: "post",
      data: {
        receiveUserId: that.data.receiveUserId
      },
    
      success: function (res) {
          console.log(res);
        // 设置自己位置设置为lat2,发单方位置为lat1
        // wx.getLocation({
        //   success: function (res) {
            wx.navigateTo({
              url: '../map/map?lat1=' + that.data.lat + "&lng1=" + that.data.lng + "&lat2=" + res.data.data.lat + "&lng2=" + "&lng2=" + res.data.data.lng,
            })
          // },
        // })

        // wx.getLocation({ //获取当前经纬度
        //   altitude: true,
        //   type: 'wgs84', //返回可以用于wx.openLocation的经纬度
        //   success: function(res) {
        //     wx.openLocation({ //​使用微信内置地图查看位置。
        //       latitude: that.data.lat, //要去的纬度-地址
        //       longitude: that.data.lng, //要去的经度-地址
        //     })
        //     wx.hideLoading()
        //     console.log("成功")
        //   },fail(res){
        //     wx.hideLoading()
        //   }
        // })

      }

    })
  },
  // map: function () {
  //   var that = this;
  //   wx.showLoading({
  //     title: '加载中',
  //     success: function () {
  //       that.setData({
  //         page: false
  //       })
  //     }
  //   })
  //   wx.request({
  //     url: 'https://www.xiaozheng8.com/receiveUser/findByReceiveUserId',
  //     method: "post",
  //     data: {
  //       receiveUserId: that.data.receiveUserId
  //     },
  //     success: function (res) {

  //       console.log(res);
  //       console.log(res.data.data.lat);
  //       console.log(res.data.data.lng);
  //       var lat = res.data.data.lat;
  //       var lng = res.data.data.lng;
  //       // 获取施工地点位置
  //       // wx.getLocation({ //获取当前经纬度
  //       //   type: 'wgs84', //返回可以用于wx.openLocation的经纬度，官方提示bug: iOS 6.3.30 type 参数不生效，只会返回 wgs84 类型的坐标信息  
  //       //   success: function (res) {
  //       //     wx.hideLoading({
  //       //       success: function () {
  //       //         that.setData({
  //       //           page: true
  //       //         })
  //       //       }
  //       //     })
  //       //     wx.openLocation({ //​使用微信内置地图查看位置。
  //       //       latitude: lat, //要去的纬度-地址
  //       //       longitude: lng, //要去的经度-地址
  //       //     })
  //       //   }
  //       // })

  //     }

  //   })
  // },
  // 通过日志获取时间和文字介绍
  myLog: function () {
    var that = this;
    // console.log(that.data.processData);
    wx.request({
      url: 'https://www.xiaozheng8.com/serviceOrder/log/v2',
      method: "post",
      data: {
        orderId: that.data.orderId,
        userType: 0
      },
      success: function (res) {
        var time = [];
        // console.log(res.data.data);
        time = res.data.data
        // console.log(time);
        that.setData({
          getTime: time,
          top: res.data.msg
        });
        // console.log(that.data.getTime);
        // console.log(that.data.processData);
        for (var i = 0; i < that.data.getTime.length; i++) {
          that.data.processData[i].name = that.data.getTime[i].des;
          if (that.data.getTime[i].isOk == true) {
            that.data.processData[i].time = that.data.getTime[i].time;
            that.setData({
              processData: that.data.processData
            });
            if (i + 1 == that.data.getTime.length) {
              that.data.processData[i].time = that.data.getTime[i - 1].time;
              that.setData({
                processData: that.data.processData
              });
            }
          }
          that.setData({

            processData: that.data.processData
          });
          console.log(that.data.processData);
          wx.hideLoading({
            success: function (res) {
              that.setData({
                page: true
              })
            }
          });
        }
      }

    });
  },

  onPullDownRefresh: function () {
    this.myData();
    // if (this.data.status == 1) {
    //   that.setData({
    //     order: wx.getStorageSync("orderDetailId")
    //   });
    // }
    setTimeout(() => {
      wx.stopPullDownRefresh()
    }, 2000)
  }
})